package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.model.McpCarDealerInformation;
import com.pgytesting.secondhandcar.model.McpUserInfo;
import com.pgytesting.secondhandcar.model.Role;
import com.pgytesting.secondhandcar.dao.McpCarDealerInformationDao;
import com.pgytesting.secondhandcar.dao.McpUserInfoDao;
import com.pgytesting.secondhandcar.dao.RoleDao;
import com.pgytesting.secondhandcar.service.RoleService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2019-08-16 15:20:21
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;
    @Autowired
    private McpUserInfoDao mcpUserInfoDao;
    @Autowired
    private McpCarDealerInformationDao mcpCarDealerInformationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer id) {
        return this.roleDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Role> queryAllByLimit(int offset, int limit) {
        return this.roleDao.queryAllByLimit(offset, limit);
    }
    
    /**
     * 条件查询多条数据
     *
     * @param role 实例对象
     * @return 对象列表
     */
    @Override
    public List<Role> queryAll(Role role) {
        return this.roleDao.queryAll(role);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Role insert(Role role) {
    	role.setStatus(0);
    	role.setCreatedBy(SessionUtil.getSessionAdmin().getId().toString());
    	role.setCreatedTime(UtilMethod.unixLong());
        this.roleDao.insert(role);
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public Role update(Role role) {
    	role.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
        this.roleDao.update(role);
        return this.queryById(role.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteById(Integer id) {
        return this.roleDao.deleteById(id) > 0;
    }

	@Override
	public Role queryObj(Role role) {
		role.setStatus(0);
		return roleDao.queryObj(role);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteObj(Integer id) {
		Role role = new Role();
		role.setId(id);
		role.setStatus(-1);
		role.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		this.update(role);
		
		McpUserInfo m = new McpUserInfo();
		m.setRoleId(id);
		List<McpUserInfo> l = mcpUserInfoDao.queryAll(m);
		
		if(l != null && l.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<l.size();i++) {
				sb.append(l.get(i).getUserNumber());
			}
			
			McpUserInfo mu = new McpUserInfo();
			mu.setUserNumbers(sb.toString());
			mu.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
			mcpUserInfoDao.updateUserRole(mu);
		}
		
		McpCarDealerInformation mc = new McpCarDealerInformation();
		mc.setRoleId(id);
		List<McpCarDealerInformation> li = mcpCarDealerInformationDao.queryAll(mc);
		
		if(li != null && li.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<li.size();i++) {
				sb.append(li.get(i).getUserNumber());
			}
			
			McpCarDealerInformation mi = new McpCarDealerInformation();
			mi.setUserNumbers(sb.toString());
			mi.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
			mcpCarDealerInformationDao.updateSaleRole(mi);
		}
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateUserRole(McpUserInfo mcpUserInfo) {
		McpUserInfo m = new McpUserInfo();
		m.setRoleId(mcpUserInfo.getRoleId());
		List<McpUserInfo> l = mcpUserInfoDao.queryAll(m);
		
		if(l != null && l.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<l.size();i++) {
				sb.append(l.get(i).getUserNumber());
			}
			
			McpUserInfo mu = new McpUserInfo();
			mu.setUserNumbers(sb.toString());
			mu.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
			mcpUserInfoDao.updateUserRole(mu);
		}
		
		m.setUserNumbers(mcpUserInfo.getUserNumbers());
		m.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		mcpUserInfoDao.updateUserRole(m);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateSaleRole(McpCarDealerInformation mcpCarDealerInformation) {
		McpCarDealerInformation m = new McpCarDealerInformation();
		m.setRoleId(mcpCarDealerInformation.getRoleId());
		List<McpCarDealerInformation> l = mcpCarDealerInformationDao.queryAll(m);
		
		if(l != null && l.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<l.size();i++) {
				sb.append(l.get(i).getUserNumber());
			}
			
			McpCarDealerInformation mi = new McpCarDealerInformation();
			mi.setUserNumbers(sb.toString());
			mi.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
			mcpCarDealerInformationDao.updateSaleRole(mi);
		}
		
		m.setUserNumbers(mcpCarDealerInformation.getUserNumbers());
		m.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		mcpCarDealerInformationDao.updateSaleRole(m);
	}
}